﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MethodWorx.CMS.UI.Models;
using MethodWorx.CMS.Content;
using MethodWorx.Core.DomainModel;
using MethodWorx.CMS.Plugins;
using MethodWorx.CMS.UI.Models.Shared;
using MethodWorx.CMS.Services;
using MethodWorx.CMS.UI.Application.ActionResults;
using MethodWorx.CMS.Plugins.ActionResults;

namespace MethodWorx.CMS.UI.Controllers
{
    public class AbstractPageTemplateController : Controller
    {
        private SchemaService service;
        //
        // GET: /AbstractPageTemplate/
        public AbstractPageTemplateController(IUnitOfWorkFactory unitOfWorkFactory, IPageTemplateRepository pageTemplates, IContentTemplateRepository contentTemplates)
        {
            this.service = new SchemaService(unitOfWorkFactory, pageTemplates, contentTemplates);
        }

        [HttpGet] 
        public ActionResult CreateFromTemplate()
        {
            return View("CreateFromTemplate", new CreateAbstractPageTemplateModel(this.service, null, true));
        }

        [HttpGet]
        public ActionResult Create(Guid? id, bool? masterPage)
        {
            return View("Create", new CreateAbstractPageTemplateModel(this.service, id, masterPage ?? true));
        }

        [HttpPost]
        public ActionResult Create(Guid? id, [Bind(Prefix="PageTemplateBasicDetails")]PageTemplateBasicDetails details, string submit, bool? masterPage)
        {
            ActionResult result = null;
            switch (submit)
            {
                case "save":
                    var model = new CreateAbstractPageTemplateModel(this.service, id, details, masterPage ?? true);

                    if (this.ModelState.IsValid)
                    {
                        try
                        {
                            //
                            //  pass to the service
                            this.service.CreatePageTemplate(
                                details.Name,
                                details.Description,
                                false, details.Parent, masterPage ?? true);
                        }
                        catch (BusinessRuleException bre)
                        {
                            foreach (var error in bre.Errors)
                            {
                                //  add the model error here
                                this.ModelState.AddModelError(error.Key, error.Message);
                            }
                        }
                        //
                        //  lets use the service
                        result = new RefreshTreeNodeResult().And(new CloseDialogResult());
                    }
                    else
                        result = View("Create", model);
                    break;
                case "cancel":
                    result = new CloseDialogResult();
                    break;
            }

            return result;
        }
    }
}
